"use client"

// 模拟API接口，实际项目中可以对接真实的新闻API
export class NewsAPI {
  private static baseUrl = "https://api.example.com"

  // 获取新闻列表
  static async getNews(category?: string, page = 1, limit = 20) {
    try {
      // 这里可以对接真实的新闻API，比如：
      // - 新浪财经API
      // - 腾讯新闻API
      // - 聚合数据API
      // const response = await fetch(`${this.baseUrl}/news?category=${category}&page=${page}&limit=${limit}`)

      // 模拟数据
      return {
        success: true,
        data: {
          articles: this.getMockNews(category),
          total: 100,
          page,
          limit,
        },
      }
    } catch (error) {
      console.error("获取新闻失败:", error)
      return {
        success: false,
        error: "获取新闻失败",
      }
    }
  }

  // 获取股市数据
  static async getStockData() {
    try {
      // 可以对接雅虎财经、新浪财经等API
      // const response = await fetch(`${this.baseUrl}/stocks`)

      return {
        success: true,
        data: {
          indices: [
            { name: "上证指数", value: 3245.67, change: "+1.23%" },
            { name: "深证成指", value: 12456.78, change: "-0.45%" },
            { name: "创业板指", value: 2567.89, change: "+2.15%" },
          ],
        },
      }
    } catch (error) {
      return { success: false, error: "获取股市数据失败" }
    }
  }

  // 获取政策信息
  static async getPolicyNews() {
    try {
      // 可以对接政府官网API或政策数据库
      return {
        success: true,
        data: this.getMockPolicyNews(),
      }
    } catch (error) {
      return { success: false, error: "获取政策信息失败" }
    }
  }

  // 模拟新闻数据
  private static getMockNews(category?: string) {
    const allNews = [
      {
        id: "1",
        title: "人工智能大模型商业化提速，多家企业布局垂直领域应用",
        summary: "ChatGPT引发的AI热潮持续发酵，国内外科技巨头纷纷加码投入",
        category: "科技",
        industry: "tech",
        publishTime: "2024-01-15T10:30:00Z",
        source: "科技日报",
        author: "张明华",
        tags: ["人工智能", "商业化", "投资机会"],
      },
      {
        id: "2",
        title: "央行宣布降准0.5个百分点，释放流动性约1.2万亿元",
        summary: "此次降准旨在保持流动性合理充裕，支持实体经济发展",
        category: "金融",
        industry: "finance",
        publishTime: "2024-01-15T09:15:00Z",
        source: "财经日报",
        author: "李晓东",
        tags: ["央行", "降准", "流动性"],
      },
    ]

    if (category && category !== "all") {
      return allNews.filter((news) => news.industry === category)
    }
    return allNews
  }

  // 模拟政策新闻
  private static getMockPolicyNews() {
    return [
      {
        id: "p1",
        title: "数字经济促进条例正式实施",
        summary: "为中小企业数字化转型提供政策支持",
        department: "国家发改委",
        publishTime: "2024-01-15T08:00:00Z",
        impact: "高",
        tags: ["数字经济", "政策解读", "中小企业"],
      },
    ]
  }
}

// 用户数据管理
export class UserAPI {
  private static users: any[] = []

  // 用户注册
  static async register(userData: any) {
    try {
      const user = {
        ...userData,
        id: Date.now().toString(),
        createdAt: new Date().toISOString(),
        membership: "basic",
      }
      this.users.push(user)

      // 实际项目中这里会调用后端API
      // const response = await fetch('/api/auth/register', {
      //   method: 'POST',
      //   headers: { 'Content-Type': 'application/json' },
      //   body: JSON.stringify(userData)
      // })

      return { success: true, data: user }
    } catch (error) {
      return { success: false, error: "注册失败" }
    }
  }

  // 用户登录
  static async login(email: string, password: string) {
    try {
      // 模拟登录验证
      const user = this.users.find((u) => u.email === email) || {
        id: "1",
        name: "张明华",
        email: email,
        company: "华创科技集团",
        position: "CEO",
        industry: "tech",
        membership: "vip",
      }

      return { success: true, data: user }
    } catch (error) {
      return { success: false, error: "登录失败" }
    }
  }

  // 获取用户信息
  static async getUserInfo(userId: string) {
    try {
      const user = this.users.find((u) => u.id === userId)
      return { success: true, data: user }
    } catch (error) {
      return { success: false, error: "获取用户信息失败" }
    }
  }
}

// 评论系统
export class CommentAPI {
  private static comments: any[] = [
    {
      id: "1",
      newsId: "news_001",
      userId: "user_1",
      userName: "张总",
      content: "这个分析很到位，我们公司正在考虑相关投资",
      createdAt: "2024-01-15T08:30:00Z",
      likes: 12,
    },
  ]

  // 获取评论列表
  static async getComments(newsId: string) {
    try {
      const newsComments = this.comments.filter((c) => c.newsId === newsId)
      return { success: true, data: newsComments }
    } catch (error) {
      return { success: false, error: "获取评论失败" }
    }
  }

  // 添加评论
  static async addComment(commentData: any) {
    try {
      const comment = {
        ...commentData,
        id: Date.now().toString(),
        createdAt: new Date().toISOString(),
        likes: 0,
      }
      this.comments.push(comment)
      return { success: true, data: comment }
    } catch (error) {
      return { success: false, error: "添加评论失败" }
    }
  }

  // 点赞评论
  static async likeComment(commentId: string) {
    try {
      const comment = this.comments.find((c) => c.id === commentId)
      if (comment) {
        comment.likes += 1
      }
      return { success: true, data: comment }
    } catch (error) {
      return { success: false, error: "点赞失败" }
    }
  }
}
